package yja.gzw.app.fragmentview

import android.content.Intent
import android.util.Log
import android.widget.Toast
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
import com.scwang.smart.refresh.layout.listener.OnRefreshListener
import yja.gzw.app.R
import yja.gzw.app.activity.ShengqingdianzifapiaoActivity
import yja.gzw.app.adapter.ChengjieAdapter
import yja.gzw.app.adapter.DiminAdapter
import yja.gzw.app.adapter.KaipiaoAdapter
import yja.gzw.app.base.BaseFragment
import yja.gzw.app.databinding.FragmentKaipiaoBinding
import yja.gzw.app.model.UserData
import yja.gzw.app.model.bean.KaipiaoBean
import yja.gzw.app.model.bean.OrderDatachengjie
import yja.gzw.app.model.bean.RechargeInfokaipiao
import yja.gzw.app.viewmodel.Mainviewmodel

class KaipiaoFragment :BaseFragment<Mainviewmodel, FragmentKaipiaoBinding>(R.layout.fragment_kaipiao), KaipiaoAdapter.OnItemClickListener{
    var shuju= mutableListOf<RechargeInfokaipiao>()
    private lateinit var fabiada: KaipiaoAdapter
    var listdata= mutableListOf<RechargeInfokaipiao>()
    var yeshu=1
    override fun initView() {
        viewmodel.onkaipiaowang(UserData.token,mapOf("page" to "1","pagesize" to "10","status" to "0"))

        binding.refreshLayout.setRefreshHeader(ClassicsHeader(context));
        binding.refreshLayout.setRefreshFooter(ClassicsFooter(context));
        binding.refreshLayout.setOnRefreshListener(object : OnRefreshListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                shuju.clear()
                yeshu=1
                viewmodel.onkaipiaowang(UserData.token,mapOf("page" to "1","pagesize" to "10","status" to "0"))

            }


        })

        binding.refreshLayout.setOnLoadMoreListener(object : OnLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {

                if(yeshu==((shuju.size/10)+1)){

                }else{
                    yeshu++
                }

                viewmodel.onkaipiaowang(UserData.token,mapOf("page" to yeshu.toString(),"pagesize" to "10","status" to "0"))


            }


        })




        binding.quanxuan.setOnClickListener {
            if(binding.quanxuan.isChecked){


                for (listdatum in listdata) {
                    listdatum.isisss=true
                }

                binding.review.layoutManager= LinearLayoutManager(context)
                fabiada= KaipiaoAdapter(requireContext(),listdata,true,this)
                binding.review.adapter= fabiada

                binding.xuanzhonggeshu.text="已选中${listdata.size}个"
            }else{

                for (listdatum in listdata) {
                    listdatum.isisss=false
                }
                binding.xuanzhonggeshu.text="已选中0个"
                binding.review.layoutManager= LinearLayoutManager(context)
                fabiada= KaipiaoAdapter(requireContext(),listdata,false,this)
                binding.review.adapter= fabiada

            }
        }
        binding.xiayibu.setOnClickListener {
            var shuzi=0
            var pingjie=""
            for ((index,listdatum) in listdata.withIndex()) {
                if(listdatum.isisss==true){
                    if(index==listdata.size-1){



                        pingjie=pingjie+listdatum.id


                    }else{

                            pingjie=pingjie+listdatum.id+","



                    }
                    shuzi++
                }




            }

            if(shuzi!=0){
            //todo 跳转到开发票

            val intent= Intent(context, ShengqingdianzifapiaoActivity::class.java)
                intent.putExtra("pingjie",pingjie)
            startActivity(intent)

            }else{
                Toast.makeText(context, "请选择你要开票的订单", Toast.LENGTH_SHORT).show()

            }



        }


    }

    override fun dataObservese() {


        viewmodel.onkaipiao.observe(this, Observer {
            if(it.code==1){
                binding.xuanzhonggeshu.text="已选中0个"

                if(it.data.data.size<10&&yeshu==1){

                    binding.review.layoutManager=
                        LinearLayoutManager(context)
                    fabiada= KaipiaoAdapter(requireContext(),it.data.data,false,this)
                    binding.review.adapter= fabiada
                    binding.refreshLayout.finishRefresh();
                    binding.refreshLayout.finishLoadMore()

                }else{
                    shuju.addAll(it.data.data)
                    binding.review.layoutManager=
                        LinearLayoutManager(context)
                    fabiada= KaipiaoAdapter(requireContext(),it.data.data,false,this)
                    binding.review.adapter= fabiada
                    binding.refreshLayout.finishRefresh();
                    binding.refreshLayout.finishLoadMore()
                    if(yeshu==1){
                        binding.review.scrollToPosition(0)

                    }else{
                        val shshbu=10*(yeshu-1)
                        binding.review.scrollToPosition(shshbu-2)

                    }
                }




//                binding.review.layoutManager= LinearLayoutManager(context)
//                fabiada= KaipiaoAdapter(requireContext(),it.data.data,false,this)
//                listdata.addAll(it.data.data)
//                binding.review.adapter= fabiada

            }else{
                Toast.makeText(context,it.msg, Toast.LENGTH_SHORT).show()

            }


        })
    }

    override fun onItemClick22(position: Int, isi: Boolean, list: List<RechargeInfokaipiao>) {


        if(isi==false){
            binding.quanxuan.isChecked=false


            var nuam=0
            for (rechargeInfokaipiao in list) {
                if(rechargeInfokaipiao.isisss==true){
                    nuam++
                }

            }
            binding.xuanzhonggeshu.text="已选中${nuam}个"


        }else{
            var nuam=0
            for (rechargeInfokaipiao in list) {
                if(rechargeInfokaipiao.isisss==true){
                    nuam++
                }

            }
            Log.e("数组的选中个数", nuam.toString())
            binding.xuanzhonggeshu.text="已选中${nuam}个"
            if(nuam==list.size){
                binding.quanxuan.isChecked=true

            }


        }

    }
}